package com.woniu.controller;

import com.woniu.entity.Stat;
import com.woniu.pojo.Class;
import com.woniu.pojo.Dept;
import com.woniu.service.*;
import org.apache.ibatis.type.IntegerTypeHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.MissingPathVariableException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("welcome")
public class WelcomeControoler {
    @Autowired
    DeptService deptService;
    @Autowired
    ClassService classService;
    @Autowired
    StudentService studentService;
    @Autowired
    EmpService empService;
    @Autowired
    CompanyService companyService;

    @RequestMapping("studentCount")
    @ResponseBody
    public List studentCount(){
        List<Stat> stats=new ArrayList<>();
        List<Dept> depts = deptService.selectAll();

        Integer i=0;
        for (Dept e : depts) {
            Integer count=0;
            stats.add(new Stat(e.getDeptName(),null));
            Class clazz=new Class();
            clazz.setDeptId(e.getDeptId());
            List<Class> classes = classService.selectByExample(clazz);
            for (Class cl : classes) {
                count+= studentService.countByExample(cl.getClassId());
            }
            stats.get(i).setValue(count);
            i++;
        }
        return stats;
    }

    @RequestMapping("count")
    @ResponseBody
    public Map count(){
        Map<String,Object> map=new HashMap<>();
        Integer studentCount = studentService.countByExample(null);
        Integer teacherCount = empService.countByExample();
        Integer companyCount = companyService.countByExample();
        map.put("studentCount", studentCount);
        map.put("teacherCount", teacherCount);
        map.put("companyCount", companyCount);
        return map;
    }
}
